const CloneDeep = require('../template')

console.log("=== CloneDeep 性能测试 ===")

// 测试小对象深拷贝性能
console.log(
  "小对象深拷贝性能:", (() => {
    const obj = { a: 1, b: 2, c: 3 }
    const iterations = 10000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      CloneDeep.cloneDeep(obj)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.01 // 平均每次拷贝时间应该小于0.01ms
  })()
)

// 测试中等对象深拷贝性能
console.log(
  "中等对象深拷贝性能:", (() => {
    const obj = {
      users: Array.from({ length: 100 }, (_, i) => ({
        id: i,
        name: `User${i}`,
        profile: { age: 20 + i, city: `City${i}` }
      })),
      meta: { total: 100, page: 1 },
      config: { theme: 'dark', language: 'en' }
    }

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      CloneDeep.cloneDeep(obj)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 1 // 平均每次拷贝时间应该小于1ms
  })()
)

// 测试大对象深拷贝性能
console.log(
  "大对象深拷贝性能:", (() => {
    const obj = {
      data: Array.from({ length: 1000 }, (_, i) => ({
        id: i,
        value: Math.random(),
        nested: {
          array: Array.from({ length: 10 }, (_, j) => j),
          object: { key: `value${j}` }
        }
      })),
      metadata: {
        created: new Date(),
        version: '1.0.0',
        settings: Array.from({ length: 50 }, (_, i) => ({ setting: i }))
      }
    }

    const iterations = 100

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      CloneDeep.cloneDeep(obj)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 10 // 平均每次拷贝时间应该小于10ms
  })()
)

// 测试深度嵌套对象性能
console.log(
  "深度嵌套对象性能:", (() => {
    let obj = { value: 'deep' }
    for (let i = 0; i < 20; i++) {
      obj = { level: i, nested: obj }
    }

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      CloneDeep.cloneDeep(obj)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 2 // 平均每次拷贝时间应该小于2ms
  })()
)

// 测试数组深拷贝性能
console.log(
  "数组深拷贝性能:", (() => {
    const arr = Array.from({ length: 10000 }, (_, i) => ({
      id: i,
      value: Math.random(),
      nested: { data: i * 2 }
    }))

    const iterations = 100

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      CloneDeep.cloneDeep(arr)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 5 // 平均每次拷贝时间应该小于5ms
  })()
)

// 测试循环引用性能
console.log(
  "循环引用性能:", (() => {
    const obj = { a: 1, b: 2 }
    obj.self = obj
    obj.circular = { parent: obj, value: 3 }

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      try {
        CloneDeep.cloneDeepWithCircular(obj)
      } catch (error) {
        // 忽略错误，测试性能
      }
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 1 // 平均每次拷贝时间应该小于1ms
  })()
)

// 测试自定义拷贝函数性能
console.log(
  "自定义拷贝函数性能:", (() => {
    const obj = { a: 1, b: 'test', c: { nested: 2 } }

    const iterations = 10000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      CloneDeep.cloneDeepWithCustomizer(obj, (value, key) => {
        if (key === 'b') return value.toUpperCase()
        return undefined
      })
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.02 // 平均每次拷贝时间应该小于0.02ms
  })()
)

// 测试深度限制性能
console.log(
  "深度限制性能:", (() => {
    let obj = { value: 'deep' }
    for (let i = 0; i < 50; i++) {
      obj = { level: i, nested: obj }
    }

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      CloneDeep.cloneDeepWithDepth(obj, 10)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 1 // 平均每次拷贝时间应该小于1ms
  })()
)

// 测试属性过滤性能
console.log(
  "属性过滤性能:", (() => {
    const obj = {}
    for (let i = 0; i < 1000; i++) {
      obj[`key${i}`] = i
    }

    const iterations = 100

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      CloneDeep.cloneDeepWithFilter(obj, (value, key) => {
        return value % 2 === 0
      })
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 2 // 平均每次拷贝时间应该小于2ms
  })()
)

// 测试属性映射性能
console.log(
  "属性映射性能:", (() => {
    const obj = {}
    for (let i = 0; i < 1000; i++) {
      obj[`key${i}`] = i
    }

    const iterations = 100

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      CloneDeep.cloneDeepWithMapper(obj, (value, key) => {
        return { [`new_${key}`]: value * 2 }
      })
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 3 // 平均每次拷贝时间应该小于3ms
  })()
)

// 测试验证性能
console.log(
  "验证性能:", (() => {
    const obj = { name: 'John', age: 30, email: 'john@example.com' }
    const schema = {
      name: { type: 'string', required: true },
      age: { type: 'number', min: 0, max: 150 },
      email: { type: 'string', pattern: /@/ }
    }

    const iterations = 10000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      try {
        CloneDeep.cloneDeepWithValidation(obj, schema)
      } catch (error) {
        // 忽略错误，测试性能
      }
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 0.05 // 平均每次拷贝时间应该小于0.05ms
  })()
)

// 测试统计性能
console.log(
  "统计性能:", (() => {
    const obj = {
      users: Array.from({ length: 100 }, (_, i) => ({
        id: i,
        profile: { name: `User${i}` }
      })),
      meta: { total: 100 }
    }

    const iterations = 1000

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      CloneDeep.cloneDeepWithStats(obj)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 1 // 平均每次拷贝时间应该小于1ms
  })()
)

// 测试内存使用性能
console.log(
  "内存使用性能:", (() => {
    const obj = {
      data: Array.from({ length: 1000 }, (_, i) => ({
        id: i,
        value: Math.random(),
        nested: { data: i * 2 }
      }))
    }

    const iterations = 100
    const results = []

    for (let i = 0; i < iterations; i++) {
      const cloned = CloneDeep.cloneDeep(obj)
      results.push(cloned)
    }

    // 检查内存使用是否合理
    return results.length === iterations &&
      results.every(result => result.data.length === 1000)
  })()
)

// 测试并发性能
console.log(
  "并发性能:", (() => {
    const obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }

    const iterations = 100
    const promises = []

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      promises.push(CloneDeep.cloneDeepConcurrent(obj, async (value, key) => {
        await new Promise(resolve => setTimeout(resolve, 1))
        return value * 2
      }, 5))
    }

    return Promise.all(promises).then(results => {
      const end = performance.now()
      const avgTime = (end - start) / iterations

      return results.length === iterations &&
        results.every(result => result.a === 2 && result.b === 4) &&
        avgTime < 10 // 平均每次拷贝时间应该小于10ms
    }).catch(() => false)
  })()
)

// 测试异步性能
console.log(
  "异步性能:", (() => {
    const obj = { a: 1, b: 'test', c: { nested: 2 } }

    const iterations = 1000
    const promises = []

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      promises.push(CloneDeep.cloneDeepAsync(obj, async (value, key) => {
        if (typeof value === 'string') return value.toUpperCase()
        return value
      }))
    }

    return Promise.all(promises).then(results => {
      const end = performance.now()
      const avgTime = (end - start) / iterations

      return results.length === iterations &&
        results.every(result => result.a === 1 && result.b === 'TEST') &&
        avgTime < 5 // 平均每次拷贝时间应该小于5ms
    }).catch(() => false)
  })()
)

// 测试大数据量性能
console.log(
  "大数据量性能:", (() => {
    const obj = {
      matrix: Array.from({ length: 100 }, (_, i) =>
        Array.from({ length: 100 }, (_, j) => i * j)
      ),
      metadata: {
        size: 10000,
        created: new Date(),
        data: Array.from({ length: 1000 }, (_, i) => ({ id: i }))
      }
    }

    const iterations = 10

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      CloneDeep.cloneDeep(obj)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 50 // 平均每次拷贝时间应该小于50ms
  })()
)

// 测试复杂结构性能
console.log(
  "复杂结构性能:", (() => {
    const obj = {
      users: Array.from({ length: 500 }, (_, i) => ({
        id: i,
        profile: {
          name: `User${i}`,
          settings: {
            theme: i % 2 === 0 ? 'dark' : 'light',
            notifications: {
              email: true,
              push: i % 3 === 0
            }
          }
        },
        posts: Array.from({ length: 10 }, (_, j) => ({
          id: j,
          title: `Post ${j}`,
          content: `Content ${j}`,
          tags: [`tag${j}`, `category${j % 3}`]
        }))
      })),
      meta: {
        total: 500,
        page: 1,
        config: {
          api: { baseUrl: 'https://api.example.com' },
          cache: { ttl: 3600 }
        }
      }
    }

    const iterations = 10

    const start = performance.now()
    for (let i = 0; i < iterations; i++) {
      CloneDeep.cloneDeep(obj)
    }
    const end = performance.now()

    const avgTime = (end - start) / iterations
    return avgTime < 100 // 平均每次拷贝时间应该小于100ms
  })()
)

console.log("=== CloneDeep 性能测试完成 ===")
